
// inf2csv.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "afxwinappex.h"
#include "afxdialogex.h"

#include "inf2csv.h"
#include "MainFrm.h"

#include "csvline.h"
#include "infdoc.h"
#include "PaneView.h"

#include "about.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

static	WCHAR	strInifile[MAX_PATH + 1];
static	LPCWSTR	szExtention = L".ini";

CInstApp theApp; // The one and only CInstApp object
CCommandLineInfo thecmdInfo;

// CInstApp

BEGIN_MESSAGE_MAP(CInstApp, CWinApp)
	ON_COMMAND(ID_APP_ABOUT, CInstApp::OnAppAbout)
END_MESSAGE_MAP()

void CInstApp::setInifile()
{
	* strInifile = 0;
	::GetModuleFileNameW(NULL, strInifile, MAX_PATH);
	if(!(* strInifile)) return;

	LPWSTR PointPos = ::wcsrchr(strInifile, L'.');
	if (PointPos)
	{
		LPCWSTR SlashPos = ::wcsrchr(strInifile, L'\\');
		if (SlashPos == NULL) SlashPos = ::wcsrchr(strInifile, L'/');
		if ((SlashPos == NULL) || (PointPos > SlashPos)) 
			* PointPos = 0;
	}
	::wcsncat(strInifile, szExtention, MAX_PATH);
	
	return;
}

// CInstApp construction

//	CInstApp::CInstApp() : m_nIndex(0) { }

// CInstApp initialization

BOOL CInstApp::InitInstance()
{
	setInifile();	//	form ini file
//	EnableVisualStyles();
	CWinApp::InitInstance();

	EnableTaskbarInteraction(FALSE);

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	
	if(* strInifile) m_pszProfileName = ::_wcsdup(strInifile);
//	SetRegistryKey(_T("Local AppWizard-Generated Applications"));
	LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views
	//ingleDocTemplate* pDocTemplate = new CSingleDocTemplate(
	CSingleDocTemplate* pDocTemplate = new CSingleDocTemplate(
							IDR_MAINFRAME,
							RUNTIME_CLASS(CinfDoc),
							RUNTIME_CLASS(CMainFrame), // main SDI frame window
							RUNTIME_CLASS(CPaneView));
		if (!pDocTemplate) return FALSE;

	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
//	CCommandLineInfo cmdInfo;
	ParseCommandLine(thecmdInfo);
	// (!(OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))) OnFileNew();
	if (!ProcessShellCommand(thecmdInfo)) return FALSE;

	// The one and only window has been initialized, so show and update it
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();
	// call DragAcceptFiles only if there's a suffix
	//  In an SDI app, this should occur after ProcessShellCommand
	// Enable drag/drop open
	m_pMainWnd->DragAcceptFiles();

	//	utility msg
	m_pMainWnd->PostMessageW(WM_INITIALUPDATE, 0, 0);

	// Dispatch commands specified on the command line.  Will return FALSE if
	// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
	return TRUE;
}

BOOL CInstApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
{
	BOOL bResult = TRUE;
	switch (rCmdInfo.m_nShellCommand)
	{
	// If we've been asked to open a file, call OpenDocumentFile()
	case CCommandLineInfo::FileOpen:	//	_dvk_dbg_
	//		if (!OpenDocumentFile(rCmdInfo.m_strFileName))
		m_pCmdInfo = & thecmdInfo;	//	use by handler of WM_INITIALUPDATE
		//	break; _dvk_dbg_
	case CCommandLineInfo::FileNew:
		if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
			OnFileNew();
		if (m_pMainWnd == NULL)
			bResult = FALSE;
		break;

	case CCommandLineInfo::FilePrintTo:
	case CCommandLineInfo::FilePrint:
		bResult = FALSE;
		break;

	// If we're doing DDE print or print to, start up without a new document and hide ourselves
	case CCommandLineInfo::FileDDENoShow:
		m_pCmdInfo = (CCommandLineInfo*)(UINT_PTR)m_nCmdShow;
		m_nCmdShow = SW_HIDE;
		break;

	// If we're doing DDE open, start up without a new document, but don't hide ourselves -- this causes the
	// icon in the Windows 7 taskbar to start in the wrong position and slide into the right position.
	case CCommandLineInfo::FileDDE:
		break;

	// If we've been asked to register, exit without showing UI.
	// Registration was already done in InitInstance().
	case CCommandLineInfo::AppRegister:
	case CCommandLineInfo::AppUnregister: 
			bResult = FALSE;    // that's all we do
		break;

	}
	return bResult;
}

// CInstApp message handlers

void CInstApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

int CInstApp::ExitInstance()
{
	m_pCmdInfo = NULL;
	// TODO: Add your specialized code here and/or call the base class
	return CWinApp::ExitInstance();
}

BOOL CInstApp::OnIdle(LONG lCount)	//	FALSE: no job
{
	// TODO: Add your specialized code here and/or call the base class
	int retval = CWinApp::OnIdle(lCount);
	if (retval) return retval;

	try {
		CDocument * pDoc = ((CFrameWnd *) m_pMainWnd)->GetActiveDocument();
		retval = ((CinfDoc *) pDoc)->pasteNextLine(m_pMainWnd->operator HWND());
	}
	catch(...) { retval = FALSE; }

	return retval;
}

BOOL CInstApp::DelRecentFile(LPCWSTR avalue)
{
	if (!m_pRecentFileList || !avalue) return FALSE;

	int nCount = m_pRecentFileList->GetSize();
	for (int i(0); i < nCount; ++i)
	{
		CString thestr = m_pRecentFileList->operator[](i);
		if (!(thestr.Compare(avalue)))
		{
			m_pRecentFileList->Remove(i);
			return TRUE;
		}
	}
	return FALSE;
}

CDocument * CInstApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
	return OpenDocumentFile(lpszFileName, TRUE);
}

CDocument * CInstApp::OpenDocumentFile(LPCTSTR lpszFileName, BOOL bAddToMRU)
{
	CDocument * pDoc = ((CFrameWnd *) m_pMainWnd)->GetActiveDocument();
	if (pDoc && lpszFileName) 
	if(((CinfDoc *) pDoc)->OnOpenRecentFile(lpszFileName, bAddToMRU))
		return pDoc;

	return NULL;
}
